<template>
    <view>
        <view class="videobox">
            <view class="goback padding-tb-xs flex align-center" @tap="gotoBack">
                <text class="cuIcon-back lg text-white"></text>
                返回
            </view>
            <mp-video-swiper
                class="video-swiper"
                v-if="isshowdom"
                id="videocomponent"
                :index="current_index"
                :allowplay="allowplay"
                :video-list="list"
                @getid="getid($event, { tagId: 'videocomponent' })"
                @getplayid="getplayid($event, { tagId: 'videocomponent' })"
                @getplaying="getplaying($event, { tagId: 'videocomponent' })"
                @setread="setread($event, { tagId: 'videocomponent' })"
                @play="onPlay($event, { tagId: 'videocomponent' })"
                @pause="onPause($event, { tagId: 'videocomponent' })"
                @change="onChange($event, { tagId: 'videocomponent' })"
                @timeupdate="onTimeUpdate($event, { tagId: 'videocomponent' })"
                @ended="onEnded($event, { tagId: 'videocomponent' })"
                @error="onError($event, { tagId: 'videocomponent' })"
                @waiting="onWaiting($event, { tagId: 'videocomponent' })"
                @progress="onProgress($event, { tagId: 'videocomponent' })"
                @UpdataVideo="onUpdataVideo($event, { tagId: 'videocomponent' })"
                @loadedmetadata="onLoadedMetaData($event, { tagId: 'videocomponent' })"
            ></mp-video-swiper>

            <view :class="'videobottomdh padding animation-slide-bottom ' + (isControls == true && allowplay == true ? 'videobottom2' : 'videobottom')">
                <!-- <view class="text-lg text-bold">{{videoTitle}}</view> -->
                <view class="flex align-center margin-top-xs" v-if="playtitle">
                    <label class="cuIcon-recordfill sm" style="font-size: 30rpx; font-size: 16px; margin-top: 10rpx; margin-right: 11rpx"></label>
                    <label class="text-sm">{{ playtitle }}</label>
                    <!-- <text class="margin-lr">|</text>
      <text class="" bindtap="openTopup">选集 ></text> -->
                </view>
            </view>
        </view>
        <view :class="'mymodal cu-modal bottom-modal ' + (showModal == true ? 'show' : '') + ' mymodal'" v-if="showModal">
            <view class="cu-dialog" catchtap>
                <view class="cu-bar bg-white padding-right-sm">
                    <view class="text-black text-left padding-left-sm">
                        <view class="text-bold">{{ videoTitle }}</view>
                        <view class="text-gray text-sm">更新至{{ recent }}</view>
                    </view>
                    <view class="mymodalclose action text-gray" @tap="hideModal"><text class="cuIcon-playfill text-gray"></text></view>
                </view>
                <view class="padding-sm">
                    <view class="checkjs flex align-center flex-wrap">
                        <view @tap="changeBot" :data-index="index" :class="'checkjsitem ' + (index == botindex ? 'on' : '')" v-for="(item, index) in cflist" :key="index">
                            {{ item[0].chapnum }}-{{ item[item.length - 1].chapnum }}
                        </view>
                    </view>
                    <view class="jslist flex align-center flex-wrap">
                        <view
                            class="jslistitem"
                            @tap="gotoPlay"
                            :data-playnum="item.chapnum"
                            :data-myindex="item.myindex"
                            :data-id="item.id"
                            v-for="(item, index) in cflist[botindex]"
                            :key="item.id"
                        >
                            <view class="lockbg flex align-center justify-center" v-if="item.is_sale == 0 && item.ispayd == 1">
                                <text class="cuIcon-lock text-gray" style="font-size: 50rpx"></text>
                            </view>

                            <view class="jscont">第{{ item.chapnum }}集</view>

                            <image :src="item.vertical_cover" mode="widthFix"></image>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <view class="addzj text-white" v-if="isfav == 0" @tap="addAfter">
            <text class="cuIcon-record lg text-white" style="font-size: 50rpx"></text>
            <view class="text-sm">追剧</view>
        </view>
        <view class="addxj text-white" @tap="openTopup">
            <text class="cuIcon-list lg text-white" style="font-size: 50rpx"></text>
            <view class="text-sm">选集</view>
        </view>
        <view class="controlbox" v-if="isControls && allowplay == true">
            <view class="flex align-center justify-center">
                <view class="playbtnbox flex align-center justify-center" @tap="playorstop">
                    <text class="cuIcon-playfill lg text-white" v-if="!isPlaying" style="font-size: 50rpx"></text>
                    <text class="cuIcon-stop lg text-white" v-if="isPlaying" style="font-size: 50rpx"></text>
                </view>
            </view>
            <view class="flex align-center justify-center margin-top-lg">
                <view class="text-white text-sm">{{ currentTimestr }}</view>
                <view style="width: 70%">
                    <slider
                        @change="sliderChange"
                        block-size="18"
                        @changing="sliderChanging"
                        :value="sliderValue"
                        backgroundColor="#A8A8A8"
                        activeColor="#4fadf8"
                        block-color="#ffffff"
                    />
                </view>
                <view class="text-white text-sm">{{ durationstr }}</view>
            </view>
        </view>

        <view :class="'cu-modal bottom-modal ' + (showPay == true ? 'show' : '') + ' mymodal'" @tap="hidePay">
            <view class="cu-dialog" catchtap>
                <view class="cu-bar cztopbox padding-right-sm">
                    <view class="action text-black">K币充值</view>
                    <view class="action text-gray" v-if="getuser">
                        账户余额：
                        <text class="text-black">{{ getuser.virtual_currency }}</text>
                        K币
                    </view>
                </view>
                <view class="padding padding-bottom-xs padding-lr-xl flex align-center justify-between flex-wrap">
                    <view
                        :class="'topupitem  flex flex-direction align-center justify-between ' + (item.is_vip == 1 ? 'vip' : '')"
                        @tap="checkPay"
                        :data-id="item.id"
                        v-for="(item, index) in product"
                        :key="index"
                    >
                        <view class="pushbox" v-if="item.corner_mark">{{ item.corner_mark }}</view>

                        <view class="sale text-bold text-xl flex align-center">{{ item.pay_money }}元</view>

                        <view class="kbi text-sm margin-top-xs">
                            <text v-if="item.sub_title">{{ item.sub_title }}{{ item.virtual_currency_gift != 0 ? '+送' + item.virtual_currency_gift + '币' : '' }}</text>
                            <text v-if="item.sub_title_end" class="margin-left-xs" style="color: rgb(250, 96, 130)">{{ item.sub_title_end }}</text>
                        </view>

                        <view class="song flex align-center justify-center text-sm margin-top-xs text-red">
                            {{ item.remark }}
                        </view>
                    </view>
                    <view>虚拟商品购买后不可退换，未成年请在监护人陪同下充值</view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import mpVideoSwiper from '../../components/video-swiper/index';
const _api = require('../../utils/api.js');
const apiurl = require('../../utils/apiurl/public.js');
const urls = [
    {
        id: 1,
        url: 'http://img.qiuqiumedia.com/uploads/video/20230517/820w712175339.m3u8',
        objectFit: 'cover',
        allowPlay: true
    },
    {
        id: 2,
        url: 'http://img.qiuqiumedia.com/uploads/video/20230517/ggya8z2180700.m3u8',
        objectFit: 'cover',
        allowPlay: true
    },
    {
        id: 3,
        url: 'http://img.qiuqiumedia.com/uploads/video/20230517/y428gld180536.m3u8',
        objectFit: 'cover',
        allowPlay: false
    },
    {
        id: 4,
        url: 'http://img.qiuqiumedia.com/uploads/video/20230517/0v23ffx180446.m3u8',
        objectFit: 'cover',
        allowPlay: false
    },
    {
        id: 5,
        url: 'http://img.qiuqiumedia.com/uploads/video/20230517/l6zefzd180205.m3u8',
        objectFit: 'cover',
        allowPlay: false
    },
    {
        id: 6,
        url: 'http://img.qiuqiumedia.com/uploads/video/20230517/51r8ok5175928.m3u8',
        objectFit: 'cover',
        allowPlay: false
    }
];
const videoList = [];
export default {
    components: {
        mpVideoSwiper
    },
    data() {
        return {
            playtitle: '',
            currentTimestr: '',

            //当前时间
            durationstr: '',

            //总时长
            currentTime: null,

            //当前时间
            duration: null,

            //总时长
            sliderValue: 0,

            //进度条的值
            updateState: false,

            //防止视频播放过程中导致的拖拽失效
            isPlaying: false,

            //是否正在播放
            videoList: [],

            playId: 1,
            current_index: 0,

            // 这个参数可以控制从第几个视频开始播放（可以从onLoad动态修改这个参数）
            videoId: 0,

            //视频id
            list: [],

            //集数列表
            showlist: [],

            //集数列表
            cflist: [],

            //拆分后的集数列表
            isControls: false,

            videoTitle: '',

            //视频标题
            poster: '',

            //视频图片
            recent: '',

            //最新集数
            isfav: 0,

            //是否已加入追剧
            showPay: false,

            //是否显示底部充值
            allowplay: false,

            //是否允许播放
            showModal: false,

            botindex: 0,

            //底部选集展示的index
            isshowdom: false,

            clickid: '',
            myindex: null,

            //自定义下标
            //从接口获取的用户信息
            getuser: null,

            product: '',
            xb: false,
            xiabiao: 0
        };
    },
    onLoad(options) {
        this.setData({
            videoId: options.id
        });
        console.log('options', options);
        if (options.num) {
            console.log(options.num);
            this.setData({
                current_index: options.num - 1,
                myindex: options.num - 1
            });
        }
        if (options.channel_id) {
            uni.setStorageSync('channelid', options.channel_id);
        }
        if (options.link_id) {
            uni.setStorageSync('linkid' + options.id, options.link_id);
            uni.setStorageSync('linkid', options.link_id);
        } else {
            if (uni.getStorageSync('linkid' + options.id)) {
                uni.setStorageSync('linkid', uni.getStorageSync('linkid' + options.id));
            } else {
                uni.setStorageSync('linkid', 0);
            }
        }

        //this.getCode()
        this.getDetial();
        this.getChapter();
        this.getUserinfo();
        if (options.clickid) {
            this.setData({
                clickid: options.clickid
            });
            _api.post(apiurl.ttconversion, {
                clickid: options.clickid,
                type: 'active',
                data: JSON.stringify(options)
            }).then((loginres) => {});
        }
    },
    onHide() {
        console.log('监听页面隐藏');
        // _api.get(apiurl.chapterinfo,{video_id:this.data.videoId,chapnum:this.data.list[this.data.myindex].chapnum}).then(res=>{

        // })
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        console.log('监听页面卸载');
    },
    methods: {
        onPlay(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
        },
        onChange(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
        },

        onPause(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            //   const childComponent = this.selectComponent('#videocomponent');
            // childComponent.stopvideo();
            console.log('pause', e.detail.activeId);
        },

        onEnded(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            const childComponent = this.zpSelectComponent('#videocomponent');
            childComponent.autoNext();
        },

        onError(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
        },

        onWaiting(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            console.log('缓冲播放中', e);
        },

        onUpdataVideo(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
        },

        onTimeUpdate(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            if (this.updateState) {
                //判断拖拽完成后才触发更新，避免拖拽失效
                // console.log(e.detail.myindex)
                // console.log(this.data.myindex)
                if (this.myindex == null) {
                    this.setData({
                        myindex: 0
                    });
                }
                if (e.detail.myindex == this.myindex) {
                    // console.log(e.detail);
                    // console.log(888)
                    let sliderValue = (e.detail.currentTime / e.detail.duration) * 100;
                    this.setData({
                        sliderValue,
                        duration: e.detail.duration,
                        currentTimestr: this.getTime(e.detail.currentTime),
                        durationstr: this.getTime(e.detail.duration)
                    });
                }
            }
        },

        onProgress(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
        },

        onLoadedMetaData(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
        },

        getplaying(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            this.setData({
                isPlaying: e.detail.val
            });
            if (e.detail.val) {
                this.setData({
                    updateState: true
                });
            } else {
                this.setData({
                    updateState: false
                });
            }
        },

        getplayid(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            this.setData({
                myindex: e.detail.myindex,
                current_index: e.detail.myindex,
                // currentTimestr:'',//当前时间
                // durationstr:'',//总时长
                // currentTime:null,//当前时间
                // duration:null,//总时长
                // sliderValue:0,//进度条的值
                // updateState: false, //防止视频播放过程中导致的拖拽失效
                isControls: false
            });
            let findarr = this.list[e.detail.myindex];
            console.log('findarr', findarr);
            this.setData({
                playtitle: findarr.title
            });
            if (findarr) {
                if (!findarr.allowPlay) {
                    // this.openPay()
                    this.getChapterdetail(e.detail.myindex);
                }
            }
        },

        getid(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            // console.log(e.detail.id);
            let find = this.list[e.detail.myindex];
            if (find) {
                this.setData({
                    allowplay: find.allowPlay
                });
            }
            this.setData({
                isControls: !this.isControls
            });
            // console.log(45);
        },

        openTopup() {
            //打开选集
            this.getCode();
            this.setData({
                showModal: true
            });
        },

        gotoPlay(e) {
            // console.log(e.currentTarget.dataset.playnum);
            // console.log(e.currentTarget.dataset.id);
            // console.log(e.currentTarget.dataset.myindex);
            console.log('点击了视频', e.currentTarget);
            let findarr = this.list[e.currentTarget.dataset.myindex];
            this.setData({
                playId: e.currentTarget.dataset.id,
                myindex: e.currentTarget.dataset.myindex,
                playtitle: findarr.title
            });
            if (findarr) {
                if (!findarr.allowPlay) {
                    // this.openPay()
                    this.setData({
                        isshowdom: false
                    });
                    this.setData({
                        current_index: findarr.myindex,
                        showModal: false
                    });
                    this.setData({
                        isshowdom: true
                    });
                    this.getChapterdetail(findarr.myindex);
                } else {
                    this.setData({
                        isshowdom: false
                    });
                    this.setData({
                        current_index: findarr.myindex,
                        showModal: false
                    });
                    this.setData({
                        isshowdom: true
                    });
                }
                let _this = this;
                _api.get(apiurl.chapterinfo, {
                    video_id: this.videoId,
                    chapnum: this.list[this.myindex].chapnum
                }).then((res) => {
                    console.log('执行了', res.data.virtual_currency);
                    _this.setData({
                        'getuser.virtual_currency': res.data.virtual_currency
                    });
                });
            }
        },

        gotoPlay2(ind) {
            this.setData({
                isshowdom: false
            });
            this.setData({
                current_index: ind,
                showModal: false
            });
            // let findarr=this.data.list[playnum-1]
            //     if(findarr){
            //         if(!findarr.allowPlay){
            //             this.getChapterdetail(playnum)
            //             // this.openPay()
            //         }
            //     }
            this.setData({
                isshowdom: true
            });
            _api.get(apiurl.chapterinfo, {
                video_id: this.videoId,
                chapnum: this.list[this.myindex].chapnum
            }).then((res) => {
                console.log('res', res);
            });
        },

        sliderChange(e) {
            //完成一次拖动后触发的事件，event.detail = {value}
            console.log(this.duration);
            console.log(6666);
            if (this.duration) {
                // var videoContextCurrent = wx.createVideoContext('myVideo' + this.data.currentIndex)
                // videoContextCurrent.seek(e.detail.value / 100 * this.data.duration); //完成拖动后，计算对应时间并跳转到指定位置
                // videoContextCurrent.play()
                const childComponent = this.zpSelectComponent('#videocomponent');
                childComponent.gotoTime(e.detail.value, this.duration);
                this.setData({
                    sliderValue: e.detail.value,
                    isPlaying: true,
                    updateState: true //完成拖动后允许更新滚动条
                });
            }
        },

        sliderChanging(e) {
            //拖动过程中触发的事件，event.detail = {value}
            const childComponent = this.zpSelectComponent('#videocomponent');
            childComponent.stopvideo();
            this.setData({
                updateState: false,
                //拖拽过程中，不允许更新进度条
                isPlaying: false
            });
        },

        playorstop() {
            const childComponent = this.zpSelectComponent('#videocomponent');
            childComponent.playvideo();
        },

        getTime(time) {
            //秒数转为分秒
            // 转换为式分秒
            let h = parseInt((time / 60 / 60) % 24);
            h = h < 10 ? '0' + h : h;
            let m = parseInt((time / 60) % 60);
            m = m < 10 ? '0' + m : m;
            let s = parseInt(time % 60);
            s = s < 10 ? '0' + s : s;
            // 作为返回值返回
            return m + ':' + s;
        },

        addAfter() {
            _api.post(apiurl.addfavorite, {
                video_id: this.videoId
            }).then((res) => {
                if (res.data.code == 0) {
                    uni.showToast({
                        title: '加入成功！'
                    });
                    setTimeout(() => {
                        uni.hideToast();
                        this.setData({
                            isfav: 1
                        });
                    }, 1000);
                }
            });
        },

        hideModal() {
            //关闭选集
            this.setData({
                showModal: false
            });
        },

        changeBot(e) {
            //切换底部的剧集分组

            // console.log(e.currentTarget.dataset.index)
            this.setData({
                botindex: e.currentTarget.dataset.index
            });
        },

        getDetial() {
            //或取视频详情
            uni.showLoading({
                title: '数据加载中'
            });
            _api.get(apiurl.videoinfo, {
                id: this.videoId,
                page: 1,
                limit: 20
            }).then((res) => {
                uni.hideLoading();
                if (res.data.code == 0) {
                    this.setData({
                        videoTitle: res.data.data.title,
                        poster: res.data.data.vertical_cover,
                        recent: res.data.data.chapter_recent,
                        isfav: res.data.data.is_fav,
                        product: res.data.data.data.data
                    });
                    console.log('-------------', res.data.data);
                }
            });
        },

        getChapter() {
            //获取视频的集数列表
            uni.showLoading({
                title: '数据加载中'
            });
            this.setData({
                isshowdom: false
            });
            _api.get(apiurl.chapterindex, {
                video_id: this.videoId,
                page: 1,
                limit: 200
            }).then((res) => {
				console.log(res)
                if (res.data.code == 0) {
                    let zslist = [];
                    zslist = res.data.data.map((item, index) => {
                        item.url = item.video_url;
                        item.id = item.chapnum;
                        item.myindex = index;
                        item.idxKey = item.chapnum;
                        item.objectFit = 'contain';
                        if (item.ispayd === 1) {
                            if (item.is_sale === 1) {
                                item.allowPlay = true;
                            } else {
                                item.allowPlay = false;
                            }
                        } else {
                            item.allowPlay = true;
                        }
                        return item;
                        // zslist.push(item)
                    });

                    let zfdata = this.splitArray(zslist, 9); //拆分数组
                    this.setData({
                        list: zslist,
                        cflist: zfdata,
                        playtitle: zslist[0].title,
                        isshowdom: true
                    });
                    // console.log(this.data.list);
                    if (this.haveparams) {
                        //   this.getChapterdetail(this.data.paramsnum)
                    } else {
                        //   this.getChapterdetail(0)
                    }
                    uni.hideLoading();
                }
            });
        },

        splitArray(array, size) {
            let data = [];
            for (let i = 0; i < array.length; i += size) {
                data.push(array.slice(i, i + size));
            }
            return data;
        },

        changeControls() {
            //修改是否展示视频控制
            let nucontrol = !this.isControls;
            this.setData({
                isControls: nucontrol
            });
        },

        openPay() {
            //打开充值框
            this.setData({
                showPay: true
            });
        },

        hidePay() {
            //关闭充值框
            this.setData({
                showPay: false
            });
        },

        getProduct() {
            //获取充值项目
            _api.get(apiurl.rechargeitem).then((res) => {
                if (res.data.code == 0) {
                    this.setData({
                        product: res.data.data
                    });
                }
            });
        },

        getCode() {
            uni.login({
                success: (res) => {
                    var code = res.code;
                    if (code) {
                        uni.getUserInfo({
                            success: (ures) => {
                                _api.post(apiurl.login, {
                                    js_code: code,
                                    encryptedData: ures.encryptedData,
                                    iv: ures.iv
                                }).then((loginres) => {
                                    console.log(232323);
                                    console.log(loginres.data);
                                    if (loginres.data.code == 0) {
                                        uni.setStorageSync('token', loginres.data.data.token);
                                        this.setData({
                                            allowplay: true
                                        });
                                        this.getUserinfo();
                                    } else {
                                        uni.showToast({
                                            title: '登录失败！',
                                            icon: 'error'
                                        });
                                        setTimeout(() => {
                                            uni.hideToast();
                                        }, 1500);
                                    }
                                });
                            }
                        });
                    }
                }
            });
        },

        getUserinfo() {
            //获取用户信息
            _api.get(apiurl.userinfo).then((res) => {
                if (res.data.code == 0) {
                    this.setData({
                        getuser: res.data.data
                    });
                    uni.setStorageSync('getuser', res.data.data);
                    const obj = uni.getLaunchOptionsSync();
                    const query = obj.query; // 包含地址栏参数的对象
                    let params = {
                        clue_token: query.clue_token,
                        project_id: query.project_id,
                        promotion_id: query.promotion_id,
                        advertiser_id: query.advertiser_id,
                        req_id: query.req_id,
                        data: JSON.stringify(query),
                        mid1: query.mid1,
                        mid2: query.mid2,
                        mid3: query.mid3,
                        mid4: query.mid4,
                        mid5: query.mid5,
                        type: 'play',
                        data: JSON.stringify(query)
                    };
                    if (params.clue_token && params.project_id) {
                        _api.post('index/user/data_report', params).then((qdres) => {});
                    }
                }
                uni.hideLoading();
            });
        },

        gotoBack() {
            //退出页面前提示
            const pages = getCurrentPages();
            if (pages.length > 1) {
                uni.navigateBack({
                    delta: 1 // 返回的页面数，如果是1就是返回上一页
                });
            } else {
                uni.switchTab({
                    url: '/pages/list/list'
                });
            }
        },

        checkPay(e) {
            //选择充值的项
            let id = e.currentTarget.dataset.id;
            let _this = this;
            _api.post(apiurl.ordercreate, {
                id: id,
                clickid: _this.clickid
            }).then((res) => {
                if (res.data.code == 0) {
                    const SDKVersion = uni.getSystemInfoSync().SDKVersion;
                    console.log('SDKVersion0', SDKVersion, _this.compareVersion(SDKVersion, '2.19.2'));
                    if (_this.compareVersion(SDKVersion, '2.19.2') >= 0) {
                        let da = res.data.data;
                        let orderid = JSON.parse(da.signData).outTradeNo;
                        uni.requestVirtualPayment({
                            signData: JSON.stringify(JSON.parse(da.signData)),
                            paySig: da.paySig,
                            signature: da.signature,
                            mode: da.mode,
                            success(res) {
                                console.log('虚拟支付支付成功', res);
                                // wx.showLoading({
                                //   title: '查询状态中',
                                // })
                                _this.wxorder(orderid, _this.current_index);
                                // _this.getProduct()
                                // _this.getChapterdetail(_this.data.myindex)
                            },

                            fail({ errMsg, errCode }) {
                                console.log('errCode', errCode);
                                _api.post(apiurl.wxlog, {
                                    code: JSON.stringify(errCode),
                                    text: JSON.stringify(errMsg)
                                }).then((loginres) => {});
                                let sd = {
                                    currentTarget: {
                                        dataset: {
                                            id: id
                                        }
                                    }
                                };
                                if (
                                    JSON.stringify(errCode) === '-15003' ||
                                    JSON.stringify(errCode) === '-5' ||
                                    JSON.stringify(errCode) === '-15002' ||
                                    JSON.stringify(errCode) === '-15007' ||
                                    JSON.stringify(errCode) === '-15012'
                                ) {
                                    _this.checkPay(sd);
                                }
                            }
                        });
                    } else {
                        console.log('当前用户的客户端版本不支持 wx.requestVirtualPayment');
                        uni.showToast({
                            title: '请升级微信，当前版本不支持购买',
                            icon: 'none',
                            duration: 3000
                        });
                    }
                    // JSON.stringify({
                    //   offerId: da.offerId,
                    //   buyQuantity: da.buyQuantity,
                    //   env:  da.env,
                    //   currencyType: da.currencyType,
                    //   platform: da.platform,
                    //   productId:da.attach.id,
                    //   goodsPrice:da.goodsPrice,
                    //   outTradeNo: da.outTradeNo,
                    //   attach: da.attach,
                    // })
                    // wx.requestPayment({
                    //     timeStamp: res.data.data.timeStamp,
                    //     nonceStr: res.data.data.nonceStr,
                    //     package: res.data.data.package,
                    //     signType: res.data.data.signType,
                    //     paySign: res.data.data.paySign,
                    //     success (res) {
                    //         console.log(res);
                    //         console.log("成功");
                    //         _this.setData({
                    //             showPay:false
                    //         })
                    //         _this.hideModal()
                    //         _this.getProduct()
                    //         console.log(_this.data.myindex)
                    //         console.log(_this.data.current_index)
                    //         // _this.getChapterdetail(_this.data.myindex)
                    //         _this.getChapterdetail(_this.data.current_index)
                    //         _this.getUserinfo()
                    //     },
                    //     fail (res) {
                    //         console.log(res);
                    //         console.log("失败");
                    //     }
                    //   })
                } else if (res.data.code == 103) {
                    this.getCode();
                }
            });
        },

        wxorder(id, index) {
            let _this = this;
            _api.post(apiurl.wxorder, {
                orderid: id
            }).then((res) => {
                if (res.data.code == 0) {
                    let data = res.data.data.order;
                    if (data.status <= 1) {
                        setTimeout(() => {
                            _this.wxorder(id);
                        }, 2000);
                    }
                    uni.hideLoading();
                    _this.setData({
                        showPay: false
                    });
                    _this.getChapterdetail(index);
                    _this.hideModal();
                    _this.getUserinfo();
                }
            });
        },

        compareVersion(_v1, _v2) {
            if (typeof _v1 !== 'string' || typeof _v2 !== 'string') {
                return 0;
            }
            const v1 = _v1.split('.');
            const v2 = _v2.split('.');
            const len = Math.max(v1.length, v2.length);
            while (v1.length < len) {
                v1.push('0');
            }
            while (v2.length < len) {
                v2.push('0');
            }
            for (let i = 0; i < len; i++) {
                const num1 = parseInt(v1[i], 10);
                const num2 = parseInt(v2[i], 10);
                if (num1 > num2) {
                    return 1;
                } else if (num1 < num2) {
                    return -1;
                }
            }
            return 0;
        },

        getChapterdetail(ind) {
            //获取某一集的信息
            console.log(ind);
            var _this = this;
            uni.showLoading({
                title: '数据加载中'
            });
            let hde = ind;
            let childComponent = this.zpSelectComponent('#videocomponent');
            let findobj = this.list[ind];
            if (findobj) {
                _api.get(apiurl.chapterinfo, {
                    video_id: this.videoId,
                    chapnum: findobj.chapnum
                }).then((res) => {
                    console.log('res', res);
                    uni.hideLoading();
                    if (res.data.code == 0) {
                        //如果正常播放

                        this.setData({
                            allowplay: true,
                            isPlaying: true,
                            updateState: true
                        });
                        if (this.list[ind].allowPlay) {
                            this.gotoPlay2(ind);
                        } else {
                            if (res.data.data.ispayd == 1) {
                                let xb = `list[${ind}].allowPlay`;
                                this.setData({
                                    [xb]: true
                                });
                                console.log(this.list);
                                this.gotoPlay2(ind);
                            }
                        }
                        if (this.list[hde].ispayd == 1) {
                            let ind2 = hde % 9;
                            let xiabiao = `cflist[${this.botindex}][${ind2}].is_sale`;
                            this.setData({
                                [xiabiao]: 1
                            });
                            console.log(this.cflist);
                            console.log(55555555555);
                        }
                    } else if (res.data.code == 103) {
                        //如果没有登录

                        childComponent.stopvideo();
                        this.setData({
                            isControls: false,
                            allowplay: false,
                            isPlaying: false
                        });
                        childComponent.stopvideo();
                        uni.showModal({
                            title: '提示',
                            content: '请先登录！',
                            success(res) {
                                if (res.confirm) {
                                    _this.getCode();
                                } else if (res.cancel) {
                                    console.log('用户点击取消');
                                }
                            }
                        });
                    } else if (res.data.code == 105) {
                        //如果k币不足
                        console.log(45454, res.data.virtual_currency);
                        childComponent.stopvideo();
                        this.setData({
                            isControls: false,
                            isPlaying: false,
                            allowplay: false
                        });
                        childComponent.stopvideo();
                        this.setData({
                            'getuser.virtual_currency': res.data.virtual_currency
                        });
                        let _this = this;
                        _this.openPay();
                        console.log(21212);
                        findobj;
                        this.setData({
                            playtitle: findobj.title
                        });
                    }
                });
            }
        },

        setread(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
        }
    }
};
</script>
<style>
/* @import "../common.css"; */

page {
    background-image: url('https://api.mzhapi.com/static/img/background.gif');
    background-size: cover; /* 可根据需求选择其他值，如contain等 */
    background-size: cover; /* 根据需要调整 */
    background-repeat: no-repeat;
    background-attachment: fixed; /* 可选，使背景固定 */
    background-position: center center; /* 将背景图片水平和垂直居中 */
    background-size: 320rpx 240rpx;
    background-color: #1a1a1a;
}

mp-video-swiper {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 999;
}
.goback {
    position: absolute;
    left: 0;
    top: 80rpx;
    z-index: 30000;
    color: #ffffff;
    padding-left: 20rpx;
}
.cu-dialog {
    background-color: #ffffff !important;
}
.cztopbox {
    background: linear-gradient(to bottom, rgb(0 150 136 / 21%), #ffffff);
}
.videobox {
    width: 100%;
    height: 100%;
    position: absolute !important;
    left: 0;
    top: 0;
}
.videobox video {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}
.videobottom {
    position: absolute;
    z-index: 6000;
    bottom: 0;
    left: 0;
    color: #ffffff;
}
.videobottom2 {
    position: absolute;
    z-index: 6000;
    bottom: -150rpx;
    left: 0;
    color: #ffffff;
}
.videobottomdh {
    transition: margin 0.3s;
}

.mymodal {
    z-index: 10000;
}
.mymodalclose text {
    transform: rotate(90deg);
}
.checkjsitem {
    background: #f0f0f0;
    border-radius: 8rpx;
    margin: 8rpx 10rpx;
    padding: 10rpx 16rpx;
}
.checkjsitem.on {
    background: #fd9261;
    color: #ffffff;
}

.jslist {
    max-height: 670rpx;
    overflow-y: auto;
    margin-top: 20rpx;
}
.jslistitem {
    width: 224rpx;
    margin-right: 19rpx;
    margin-bottom: 20rpx;
    position: relative;
    height: 354rpx;
    border-radius: 12rpx;
    overflow: hidden;
    position: relative;
}
.jslistitem image {
    display: block;
    object-fit: cover;
}
.jscont {
    position: absolute;
    width: 100%;
    height: 40rpx;
    text-align: center;
    left: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 1000;
    color: #ffffff;
}
.jslistitem:nth-child(3n) {
    margin-right: 0;
}

.myswiper {
    position: absolute;
    height: 100%;
    width: 100%;
}

.topupitem {
    width: 46%;
    position: relative;
    margin-bottom: 30rpx;
    border-radius: 12rpx;
    height: 186rpx;
    background: #ffffff;
    border: #e8e8e8 solid 2rpx;
}
.pushbox {
    position: absolute;
    font-size: 10px;
    color: #ffffff;
    line-height: 34rpx;
    height: 34rpx;
    background: #1cbbb4;
    padding: 0 12rpx;
    border-radius: 17rpx 0 0 17rpx;
    top: 0;
    right: 0;
}
.sale {
    color: #333333;
    height: 60rpx;
    padding-top: 40rpx;
}
.topupitem.zk {
    border: #fb151c solid 4rpx;
    background: #ffcd52;
}
.topupitem.vip {
    border: #1cbbb4 solid 4rpx;
    background: rgba(250, 96, 130, 0.1);
}
.song {
    color: #1cbbb4;
    font-weight: bold;
    height: 50rpx;
    width: 100%;
    background: #1cbbb41f;
}
.topupitem.vip .sale {
    color: #333333;
}
.topupitem.vip .song {
    color: #ffffff !important;
    background: #1cbbb4;
}
.iconbox {
    position: absolute;
    width: 90rpx;
    height: 90rpx;
    top: 0;
    right: 0;
}

.addzj {
    width: 80rpx;
    height: 80rpx;
    position: fixed;
    right: 10rpx;
    bottom: 28%;
    z-index: 999;
}
.addxj {
    width: 80rpx;
    height: 80rpx;
    position: fixed;
    right: 10rpx;
    bottom: 20%;
    z-index: 999;
}
.controlbox {
    width: 100%;
    height: 320rpx;
    position: fixed;
    left: 0;
    bottom: 0rpx;
    z-index: 9000;
}
.playbtnbox {
    width: 100rpx;
    height: 100rpx;
    background: rgb(79, 173, 248);
    border-radius: 50%;
}
.lockbg {
    position: absolute;
    z-index: 3000;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    color: #ffffff;
}
</style>
